
import {
  queryReturnOrderInfo,
  queryReturnTexts,
  submitRefundOrderMoney
} from "../../../../service/jb_order_service"
import {
  chooseMultipleOSSImgUploadQiniuAction
} from "../../../../service/upload_service"
import {
  SERVICE_URL,
  SERVICE_ID,
} from "../../../../service/base_service"
import {
  getFrontUploadSign
} from "../../../../service/config"
import debounce from "../../../../tools/debounce"

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSelectPop: false,
    showPopType: "yx",
    showPopTyepTitle: "退款原因",
    requestData: {
      text: "收货地址填错了", //退款选择的原因
      id: null, //订单id
      refund_reason_wap_explain: "",
      refund_reason_wap_img: "",//图片字符串逗号隔开 
      uni: null, //orderId order88429170520210715334424
      refund_method: 0, //0:退货退款  1：仅退款 2：退换货
    },

    reasonImgs: [

    ],//凭证图片
    customArr: [
      {
        value: "收货地址填错了",
        label: "收货地址填错了",
      },
      {
        value: "与描述不符",
        label: "与描述不符",
      },
      {
        value: "信息填错了，重新拍 ",
        label: "信息填错了，重新拍 ",
      },
      {
        value: "未按预定时间发货",
        label: "未按预定时间发货",
      },
      {
        value: "其它原因",
        label: "其它原因",
      }
    ],
    reasonTextArr: [],
    orderInfo: {
      "id": null,
      "orderId": "",
      "paid": true,
      "payPrice": "0.00",
      "totalNum": 1,
      "orderInfoList": [

      ]
    },//订单信息
    uploadData: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderId) {
      this.setData({
        'requestData.uni': options.orderId
      })
    }
    this.queryReturnTexts();
    this.queryReturnOrderInfo();
    this.getFrontUploadSign();
    setTimeout(() => {
      wx.showToast({
        title: '申请售后，请一定先联系客服协商处理，以免申请被拒绝!',
        icon: "none",
        duration: 5000,
      })
    }, 2000)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  queryReturnOrderInfo() {
    wx.showLoading();
    queryReturnOrderInfo(this.data.requestData.uni).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          orderInfo: res.data,
          'requestData.id': res.data.id
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '查询错误',
        icon: "none"
      })
      console.error("JJLog:queryReturnOrderInfo", err);
    })
  },

  queryReturnTexts() {
    queryReturnTexts().then(res => {
      if (res.code == 200) {
        this.setData({
          reasonTextArr: res.data || []
        })
        if (res.data && res.data.length > 0) {
          let arr = [];
          res.data.forEach(i => {
            let obj = {
              value: i,
              label: i
            }
            arr.push(obj)
          })
          this.setData({
            customArr: arr
          })
        }
      }
    }).catch(err => {
      console.error("JJLog:queryReturnTexts", err);
    })
  },
  applyRefundAction() {
    let pics = []
    if (this.data.reasonImgs && this.data.reasonImgs.length > 0) {
      pics = this.data.reasonImgs.map(i => i.imgUrl);
    }
    if (pics.length > 0) {
      this.setData({
        'requestData.refund_reason_wap_img': pics.join(',')
      })
    }
    console.log("JJLog:退款参数:" + JSON.stringify(this.data.requestData));
    debounce(this.submitRefundOrderMoney)
  },

  // 提交退款申请
  submitRefundOrderMoney() {
    wx.showLoading();
    submitRefundOrderMoney(this.data.requestData).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.redirectTo({
          url: '/featurePackage/pages/commonSuccess/commonSuccess?type=1',
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '申请失败',
        icon: "none"
      })
      console.error("JJLog:submitRefundOrderMoney", err);
    })
  },


  showSelectPopAction() {
    this.setData({
      showSelectPop: true,
    })
  },
  hidePopup() {
    this.setData({
      showSelectPop: false
    })
  },
  //确认选择
  sureReasonSelect(e) {
    console.log("JJLog:确认选择啦:" + JSON.stringify(e));
    this.setData({
      'requestData.text': e.detail.result.value,
      showSelectPop: false
    })
  },
  bindRemarkText(e) {
    this.setData({
      'requestData.refund_reason_wap_explain': e.detail.value
    })
  },
  changeRefundMethod(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      'requestData.refund_method': index,
    })
  },
  getFrontUploadSign() {
    getFrontUploadSign().then(res => {
      console.log("JJLog:获取到的图片签名是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.setStorageSync('ossHost', res.data.host);
        wx.setStorageSync('ossSignature', res.data.signature);
        wx.setStorageSync('ossPolicy', res.data.policy);
        wx.setStorageSync('ossAccessId', res.data.accessId);
        wx.setStorageSync('ossCallBack', res.data.callback);
        wx.setStorageSync('ossDir', res.data.dir);
        this.setData({
          uploadData: res.data,
        })
      }
    }).catch(err => {
      console.error("JJLog:getFrontUploadSign", err);
    })
  },

  //上传图片方法，多张上传哦。
  pushReplyImgAction() {
    let num = 3;
    let imgLength = this.data.reasonImgs.length;
    let arr = this.data.reasonImgs || []
    chooseMultipleOSSImgUploadQiniuAction("reply", num - imgLength, this.data.uploadData).then(res => {
      console.log("JJLog:多张图片上传:" + JSON.stringify(res));
      if (res && res.length > 0) {
        res.forEach(i => {
          arr.push(i)
        })
        this.setData({
          reasonImgs: arr
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  openServiceAction() {
    wx.openCustomerServiceChat({
      extInfo: { url: SERVICE_URL },
      corpId: SERVICE_ID,
      success(res) { },
      fail(err) {
        console.log('调用失败:', err);
      }
    })
  },
  cancelOneImg(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.reasonImgs;
    arr.splice(index, 1);
    this.setData({
      reasonImgs: arr
    })
  },


})